<!--
 * @Descripttion: 
 * @version: 
 * @Author: sueRimn
 * @Date: 2022-08-19 11:30:52
 * @LastEditors: sueRimn
 * @LastEditTime: 2022-10-10 10:52:29
-->
<template>
  <div class="component_container">
    <!-- 查询表单 -->
    <div>
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="">
          <el-input
            v-model="formInline.code"
            placeholder="请输入组件编码"
          ></el-input>
        </el-form-item>
        <el-form-item label="">
          <el-input
            v-model="formInline.description"
            placeholder="请输入组件描述"
          ></el-input>
        </el-form-item>
        <el-form-item label="">
          <el-select v-model="formInline.type" placeholder="请选择组件分类">
            <el-option label="formPage" value="formPage"></el-option>
            <el-option label="infoPage" value="infoPage"></el-option>
            <el-option label="operaterArea" value="operaterArea"></el-option>
            <el-option label="fixedLayout" value="fixedLayout"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="">
          <el-input
            v-model="formInline.author"
            placeholder="请输入组件创建人"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="addComponent">添加</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 查询结果列表显示 -->
    <el-table :data="tableData" style="width: 100%" ref="compoentTalbe">
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-form label-position="left" class="demo-table-expand">
            <el-form-item label="组件ID">
              <span>{{ props.row.id }}</span>
            </el-form-item>
            <el-form-item label="组件描述">
              <span v-if="!props.row.edit">{{ props.row.description }}</span>
              <el-input v-else v-model="props.row.description"></el-input>
            </el-form-item>
            <el-form-item label="组件排序值">
              <span v-if="!props.row.edit">{{ props.row.sort_number }}</span>
              <el-input v-else v-model="props.row.sort_number"></el-input>
            </el-form-item>
            <el-form-item label="最后修改时间">
              <span>{{ props.row.last_modified_time }}</span>
            </el-form-item>
            <el-form-item label="组件编码">
              <span v-if="!props.row.edit">{{ props.row.code }}</span>
              <el-input v-else v-model="props.row.code"></el-input>
            </el-form-item>
            <el-form-item label="组件类型">
              <span v-if="!props.row.edit">{{ props.row.type }}</span>
              <el-select
                v-else
                placeholder="请选择组件分类"
                style="float: left"
                v-model="props.row.type"
              >
                <el-option label="formPage" value="formPage"></el-option>
                <el-option label="infoPage" value="infoPage"></el-option>
                <el-option
                  label="operaterArea"
                  value="operaterArea"
                ></el-option>
                <el-option label="fixedLayout" value="fixedLayout"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="最后修改人">
              <span>{{ props.row.last_modified_author }}</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column label="组件编码" prop="code"> </el-table-column>
      <el-table-column label="组件描述" prop="description"> </el-table-column>
      <el-table-column label="组件分类" prop="type"> </el-table-column>
      <el-table-column label="操作" prop="operate">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.edit"
            type="default"
            @click="saveEditComponent(scope.row)"
            >保存</el-button
          >
          <el-button
            v-if="scope.row.edit"
            type="default"
            @click="cancelEditComponent(scope.row)"
            >取消</el-button
          >
          <el-button
            v-if="!scope.row.edit"
            type="primary"
            @click="editComponent(scope.row)"
            >编辑</el-button
          >
          <el-button type="primary" @click="delComponent(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 新增组件弹出框 -->
    <el-dialog
      title="新增组件"
      :visible.sync="addComponentDialogVisible"
      style="height: 500px"
    >
      <el-form
        :model="addComponentForm"
        label-position="left"
        label-width="80px"
        class="demo-ruleForm"
        status-icon
        :rules="addRules"
        ref="addForm"
        size="medium"
      >
        <el-form-item label="组件编码">
          <el-input
            v-model="addComponentForm.code"
            placeholder="请输入组件编码"
          ></el-input>
        </el-form-item>
        <el-form-item label="组件描述">
          <el-input
            v-model="addComponentForm.description"
            placeholder="请输入组件描述"
          ></el-input>
        </el-form-item>
        <el-form-item label="组件分类">
          <el-select
            v-model="addComponentForm.type"
            placeholder="请选择组件分类"
            style="float: left"
          >
            <el-option label="formPage" value="formPage"></el-option>
            <el-option label="infoPage" value="infoPage"></el-option>
            <el-option label="operaterArea" value="operaterArea"></el-option>
            <el-option label="fixedLayout" value="fixedLayout"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="组件排序">
          <el-input
            v-model="addComponentForm.sort_number"
            placeholder="请输入组件描述"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addComponentDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmAdd('addForm')"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: "ComponentManagement",
  data() {
    return {
      addComponentDialogVisible: false,
      formInline: {
        code: "",
        description: "",
        type: "",
        author: "",
        // id: "",
      },
      addComponentForm: {
        code: "",
        description: "",
        type: "",
        sort_number: "",
      },
      tableData: [],
      addRules: {},
    };
  },
  created() {
    this.getComponentList();
  },
  methods: {
    addComponent() {
      this.addComponentDialogVisible = true;
    },
    editComponent(row) {
      this.$set(row, "edit", true);
      this.$nextTick(() => {
        this.$refs.compoentTalbe.toggleRowExpansion(row, true);
      });
    },
    delComponent(row) {
      this.$http.component
        .delete(this.$address.baseUrl, { id: row.id })
        .then((res) => {
          if (res.status) {
            this.getComponentList();
          }
        })
        .catch((error) => {
          console.log(error);
        });
    },
    cancelEditComponent(row) {
      this.$set(row, "edit", false);
      this.$nextTick(() => {
        this.$refs.compoentTalbe.toggleRowExpansion(row, false);
      });
    },
    saveEditComponent(row) {
      let params = {
        ...row,
      };
      this.$delete(params, "edit");
      params.last_modified_author = this.$store.state.user.userInfo.login_name;
      this.$http.component
        .edit(this.$address.baseUrl, params)
        .then((res) => {
          if (res.status) {
            this.getComponentList();
          }
        })
        .catch((error) => {
          console.log(error);
        });
    },
    confirmAdd(formName) {
      this.addComponentDialogVisible = false;
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let params = {
            create_author: this.$store.state.user.userInfo.login_name,
            ...this.addComponentForm,
          };
          this.$http.component
            .add(this.$address.baseUrl, params)
            .then((res) => {
              if (res.status) {
                this.getComponentList();
              }
            })
            .catch((error) => {
              console.log(error);
            });
        }
      });
    },
    getComponentList() {
      this.$http.component
        .getList(this.$address.baseUrl, { ...this.formInline })
        .then((res) => {
          if (res.status) {
            this.tableData = res.data;
          }
        })
        .catch((error) => {
          console.log(error);
        });
    },
    search() {
      this.getComponentList();
    },
  },
};
</script>
<style scoped>
.el-form-item--small.el-form-item {
  float: left;
}
.demo-table-expand div {
  width: 45%;
  margin-left: 40px;
  margin-bottom: 0px;
}
</style>